﻿using System;
using System.Collections.Generic;
using JQuery.WebKit.Common;

namespace JQuery.WebKit.WebCommon
{
    public enum LoadEvent
    {
        NA,
        DocumentReady,
        WindowLoad
    }
    public class WebScriptResource : IDependencySort<WebScriptResource>
    {
        //public JPage Page { get; internal set; }

        public string ControlID { get; set; }
        public Type Type { get; set; }
        public ResourceType ResourceType { get; set; }
        public string Script { get; set; }
        public bool RegisterAtTheEnd { get; set; }

        public LoadEvent LoadType { get; set; }

        public WebScriptResource(Type controlType, string controlID, ResourceType resourceType, string resource)
            : this(controlType, controlID, resourceType, resource, true, LoadEvent.DocumentReady)
        {
        }

        public WebScriptResource(Type controlType, string controlID, ResourceType resourceType, string resource, bool addScriptTag) :
            this(controlType, controlID, resourceType, resource, addScriptTag, LoadEvent.DocumentReady)
        {
        }

        public WebScriptResource(Type controlType, string controlID, ResourceType resourceType, string resource, LoadEvent loadType) :
            this(controlType, controlID, resourceType, resource, true, loadType)
        {
        }

        public WebScriptResource(Type controlType, string controlID, ResourceType resourceType, string resource, bool addScriptTag, LoadEvent loadType)
        {
            Type = controlType;
            ResourceType = resourceType;
            Script = resource;
            RegisterAtTheEnd = addScriptTag;
            LoadType = loadType;
            ControlID = controlID;
            Dependencies = new List<IDependencySort<WebScriptResource>>();
        }

        public override bool Equals(object obj)
        {
            if (obj as WebScriptResource == null)
            {
                return false;
            }
            WebScriptResource target = obj as WebScriptResource;

            return this.Type.Equals(target.Type) &&
                this.ResourceType == target.ResourceType &&
                this.Script == target.Script;
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public override string ToString()
        {
            return this.Script;
        }

        public string ID { get { return Script; } }

        public bool Visited { get; set; }

        public IList<IDependencySort<WebScriptResource>> Dependencies { get; set; }

        public IDependencySort<WebScriptResource> Root { get; set; }
    }
}
